################################################################################################
# REPLICATION CODE FOR "POLITICAL AWARENESS AND THE IDENTITY-TO-POLITICS LINK IN PUBLIC OPINION"
# PHIL JONES
# 05.04.2022
################################################################################################

# Load libraries:
library(survey)
library(ggplot2)
library(cowplot)
library(patchwork)
library(tidyverse)
library(ggrepel)


# Load data files and create weighted versions:
anes <- readRDS("anes.rds")
cces <- readRDS("cces.rds")
w.anes <- svydesign(~1,data=anes,weights=~weight)
w.cces <- svydesign(~1,data=cces,weights=~weight)




################################################################################
# SET SEED, SIMULATION DEFAULTS, GGPLOT THEMES, AND CREATE HELPER FUNCTIONS 
################################################################################

set.seed(1982)

# Simulation defaults for each dataset:
aware.p <- seq(from=0, to=1, by=.01)
svymean(~age+female+married+income+educ+rel.atd+region+race+religion, w.anes, na.rm=T)
svymean(~age+female+married+income+educ+rel.atd+region+race+religion, w.cces, na.rm=T)
a.ctrl <- data.frame(race="White", religion="Mainline Protestant", lgbt=0, union.member=0, veteran=0, female=1, age=47.5, married=1, income=3.17, educ=3.01, rel.atd=2.63, region="South", year="2016", aware.p=aware.p)
c.ctrl <- data.frame(race="White", religion="Mainline Protestant", lgbt=0, union.member=0, veteran=0, female=1, age=47.9, married=1, income=2.81, educ=2.95, rel.atd=2.94, region="South", year="2016", aware.p=aware.p)


# Ggplot themes 
theme_idkn <- function(){
  theme(panel.background=element_rect(fill="gray91"), 
        axis.text=element_text(size=8, color="black") ,
        axis.title=element_text(size=12, color="black"),
        plot.title=element_text(hjust=.5, face="bold"))
}

theme_idkn2 <- function(){
  theme(panel.background=element_rect(fill="gray91"), 
        legend.position="none",
        axis.text=element_text(size=8, color="black") ,
        axis.title=element_text(size=12, color="black"),
        plot.title=element_text(hjust=.5, face="bold"), 
        plot.margin=margin(0.1,2,0.1,0.1, "cm"))
}


# Helper function to calculate and store confidence intervals
ans.ci <- function(x){
  cis <- confint(x)
  ans <- cbind(as.matrix(x), cis)
  ans <- round(ans, digits=4)
  ans <- as.data.frame(ans)
  names(ans) <- c("est", "lo", "hi")
  ans
}
  



################################################################################
# REGRESSION MODELS FOR MAIN ANALYSES
################################################################################


# CCES data, party identity
reg.c.pid7 <- svyglm(pid7~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region+year, w.cces)

# CCES data, policy preferences 
reg.c.atts <- svyglm(atts~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region+year, w.cces)

# ANES data, party identity
reg.a.pid7 <- svyglm(pid7~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region+year, w.anes)

# ANES data, policy preferences
reg.a.atts <- svyglm(atts~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region+year, w.anes)



################################################################################
# AWARENESS AND THE IDENTITY-TO-POLITICS LINK 
# LINKING LGBT IDENTITY TO POLITICS
################################################################################

# Simulate for LGBT and straight cisgender respondents across range of awareness
# Create plots for Figure 1

# (a) Policy preferences in the CCES
evs.c.atts.lgbt <- rbind(ans.ci(predict(reg.c.atts, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "lgbt", 1), type="response")))
evs.c.atts.lgbt$id.group <- c(rep("C/H", length(aware.p)), rep("LGBT", length(aware.p)))
evs.c.atts.lgbt$aware.p <- aware.p 

g.c.atts <- ggplot(evs.c.atts.lgbt, aes(x=aware.p, y=est, ymin=lo, ymax=hi, linetype=id.group, group=id.group)) +
  geom_ribbon(alpha=.2) +
  geom_line(size= 1) +
  scale_linetype_manual(values=c("longdash", "solid"))+
  scale_y_continuous(limits=c(0,1)) + scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  xlab("Political awareness percentile") + ylab("\n\nPredicted policy preferences") +
  ggtitle("(a) Policy preferences (CCES)") +
  theme_idkn()


# (b) Policy preferences in the ANES
evs.a.atts.lgbt <- rbind(ans.ci(predict(reg.a.atts, a.ctrl, type="response")),
                         ans.ci(predict(reg.a.atts, replace(a.ctrl, "lgbt", 1), type="response")))
evs.a.atts.lgbt$id.group <- c(rep("Straight cisgender respondents", length(aware.p)), rep("LGBT respondents    ", length(aware.p)))
evs.a.atts.lgbt$aware.p <- aware.p 
g.a.atts <- ggplot(evs.a.atts.lgbt, aes(x=aware.p, y=est, ymin=lo, ymax=hi, linetype=id.group, group=id.group)) +
  geom_ribbon(alpha=.2) +
  geom_line(size= 1) +
  scale_linetype_manual(values=c("solid", "longdash"))+
  scale_y_continuous(limits=c(0,1)) + scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  xlab("Political awareness percentile") + ylab("\n\nPredicted policy preferences") +
  ggtitle("(b) Policy preferences (ANES)") +
  theme_idkn()


# (c) Party ID in the CCES
evs.c.pid7.lgbt <- rbind(ans.ci(predict(reg.c.pid7, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "lgbt", 1), type="response")))
evs.c.pid7.lgbt$id.group <- c(rep("C/H", length(aware.p)), rep("LGBT", length(aware.p)))
evs.c.pid7.lgbt$aware.p <- aware.p 

g.c.pid7 <- ggplot(evs.c.pid7.lgbt, aes(x=aware.p, y=est, ymin=lo, ymax=hi, linetype=id.group, group=id.group)) +
  geom_ribbon(alpha=.2) +
  geom_line(size= 1) +
  scale_linetype_manual(values=c("longdash", "solid"))+
  scale_y_continuous(limits=c(0,1)) + scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  xlab("Political awareness percentile") + ylab("\n\nPredicted party identity") +
  ggtitle("(c) Party identity (CCES)") +
  theme_idkn()

# (d) Party ID in the ANES
evs.a.pid7.lgbt <- rbind(ans.ci(predict(reg.a.pid7, a.ctrl, type="response")),
                         ans.ci(predict(reg.a.pid7, replace(a.ctrl, "lgbt", 1), type="response")))
evs.a.pid7.lgbt$id.group <- c(rep("C/H", length(aware.p)), rep("LGBT", length(aware.p)))
evs.a.pid7.lgbt$aware.p <- aware.p 

g.a.pid7 <- ggplot(evs.a.pid7.lgbt, aes(x=aware.p, y=est, ymin=lo, ymax=hi, linetype=id.group, group=id.group)) +
  geom_ribbon(alpha=.2) +
  geom_line(size= 1) +
  scale_linetype_manual(values=c("longdash", "solid"))+
  scale_y_continuous(limits=c(0,1)) + scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  xlab("Political awareness percentile") + ylab("\n\nPredicted party identity") +
  ggtitle("(d) Party identity (ANES)") +
  theme_idkn()


# Combine four plots into Figure 1

f1_row1 <- plot_grid(
  g.c.atts + theme(legend.position="none"),
  g.a.atts + theme(legend.position="none"),
  nrow=1)

f1_row2 <- plot_grid(
  g.c.pid7 + theme(legend.position="none"),
  g.a.pid7 + theme(legend.position="none"),
  nrow=1
)

legend <- get_legend(g.a.atts + theme(legend.position="top",
                                                       legend.text=element_text(size=13),
                                                       legend.title=element_blank()) +
                                        guides(linetype=guide_legend(keywidth=unit(1.7, "cm"))))

plot_grid(legend, f1_row1, NULL, f1_row2, ncol=1, rel_heights=c(.14,1, .07,1))
ggsave("Figure1.pdf", width=9.2, height=14)


# Specific estimates discussed in text

# LGBT and straight cisgender [C/H] respondents given different levels of awareness
print(subset(evs.c.atts.lgbt, aware.p==.1|aware.p==.9), digits=2) # Policy preferences in CCES
print(subset(evs.a.atts.lgbt, aware.p==.1|aware.p==.9), digits=2) # Policy preferences in ANES
print(subset(evs.c.pid7.lgbt, aware.p==.1|aware.p==.9), digits=2) # Party identity in CCES
print(subset(evs.a.pid7.lgbt, aware.p==.1|aware.p==.9), digits=2) # Party identity in ANES





###############################################################################
# AWARENESS AND THE IDENTITY-TO-POLITICS LINK FOR OTHER GROUPS
###############################################################################

# Simulate for different identities across range of awareness
# Create plots for Figure 2

# (a) Policy preferences and religious identities
evs.c.atts.relg <- rbind(ans.ci(predict(reg.c.atts, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "religion", "Catholic"), type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "religion", "Evangelical Protestant"), type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "religion", "Jewish"), type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "religion", "Secular"), type="response")))
evs.c.atts.relg$id.group <- c(rep("Mainline\nProtestant", length(aware.p)), rep("Catholic", length(aware.p)), rep("Evangelical\nProtestant", length(aware.p)), rep("Jewish", length(aware.p)), rep("Secular", length(aware.p)))  
evs.c.atts.relg$aware.p <- aware.p  
gg.c.atts.relg <- ggplot(evs.c.atts.relg, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("Predicted policy preferences") + ggtitle("(a) Religion") +
  geom_text(data=subset(evs.c.atts.relg, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, nudge_y=c(0.01,0,0,.015,0), lineheight=.7) + #
  coord_cartesian(clip='off') +
  theme_idkn2()


# (b) Policy preferences and racial/ethnic identities
evs.c.atts.race <- rbind(ans.ci(predict(reg.c.atts, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "race", "Black"), type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "race", "Hispanic"), type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "race", "Asian"), type="response")))
evs.c.atts.race$id.group <- c(rep("White", length(aware.p)), rep("Black", length(aware.p)), rep("Hispanic", length(aware.p)), rep("Asian", length(aware.p)))  
evs.c.atts.race$aware.p <- aware.p  
gg.c.atts.race <- ggplot(evs.c.atts.race, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(b) Race and ethnicity") +
  geom_text(data=subset(evs.c.atts.race, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()


# (c) Policy preferences and gender identities
evs.c.atts.gend <- rbind(ans.ci(predict(reg.c.atts, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "female", 0), type="response")))
evs.c.atts.gend$id.group <- c(rep("Women", length(aware.p)), rep("Men", length(aware.p)))
evs.c.atts.gend$aware.p <- aware.p  
gg.c.atts.gend <- ggplot(evs.c.atts.gend, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(c) Gender") +
  geom_text(data=subset(evs.c.atts.gend, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()


# (d) Policy preferences and union membership
evs.c.atts.unin <- rbind(ans.ci(predict(reg.c.atts, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "union.member", 1), type="response")))
evs.c.atts.unin$id.group <- c(rep("Non-\nmember", length(aware.p)), rep("Union\nmember", length(aware.p)))
evs.c.atts.unin$aware.p <- aware.p  
gg.c.atts.unin <- ggplot(evs.c.atts.unin, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(d) Union membership") +
  geom_text(data=subset(evs.c.atts.unin, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, nudge_y=c(-.005,.005), lineheight=.7) + # 
  coord_cartesian(clip='off') +
  theme_idkn2()


# (e) Policy preferences and veteran status
evs.c.atts.vets <- rbind(ans.ci(predict(reg.c.atts, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts, replace(c.ctrl, "veteran", 1), type="response")))
evs.c.atts.vets$id.group <- c(rep("Non-\nveteran", length(aware.p)), rep("Veteran", length(aware.p)))
evs.c.atts.vets$aware.p <- aware.p  
gg.c.atts.vets <- ggplot(evs.c.atts.vets, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(e) Veteran status") +
  geom_text(data=subset(evs.c.atts.vets, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()





# (f) Party identity and religious identities
evs.c.pid7.relg <- rbind(ans.ci(predict(reg.c.pid7, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "religion", "Catholic"), type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "religion", "Evangelical Protestant"), type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "religion", "Jewish"), type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "religion", "Secular"), type="response")))
evs.c.pid7.relg$id.group <- c(rep("Mainline\nProtestant", length(aware.p)), rep("Catholic", length(aware.p)), rep("Evangelical\nProtestant", length(aware.p)), rep("Jewish", length(aware.p)), rep("Secular", length(aware.p)))  
evs.c.pid7.relg$aware.p <- aware.p  
gg.c.pid7.relg <- ggplot(evs.c.pid7.relg, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("Predicted party identity") + ggtitle("(f) Religion") +
  geom_text(data=subset(evs.c.pid7.relg, aware.p==1), aes(label=id.group), hjust=0, nudge_x=0.02, nudge_y=c(0.015,-.005,0,0,0), lineheight=.7) + 
  coord_cartesian(clip='off') +
  theme_idkn2()


# (g) Party identity and racial/ethnic identities
evs.c.pid7.race <- rbind(ans.ci(predict(reg.c.pid7, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "race", "Black"), type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "race", "Hispanic"), type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "race", "Asian"), type="response")))
evs.c.pid7.race$id.group <- c(rep("White", length(aware.p)), rep("Black", length(aware.p)), rep("Hispanic", length(aware.p)), rep("Asian", length(aware.p)))  
evs.c.pid7.race$aware.p <- aware.p  
gg.c.pid7.race <- ggplot(evs.c.pid7.race, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(g) Race and ethnicity") +
  geom_text(data=subset(evs.c.pid7.race, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.8) +
  coord_cartesian(clip='off') +
  theme_idkn2()


# (h) Party identity and gender identities
evs.c.pid7.gend <- rbind(ans.ci(predict(reg.c.pid7, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "female", 0), type="response")))
evs.c.pid7.gend$id.group <- c(rep("Women", length(aware.p)), rep("Men", length(aware.p)))
evs.c.pid7.gend$aware.p <- aware.p  
gg.c.pid7.gend <- ggplot(evs.c.pid7.gend, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(h) Gender") +
  geom_text(data=subset(evs.c.pid7.gend, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.8) +
  coord_cartesian(clip='off') +
  theme_idkn2()


# (i) Party identity and union membership
evs.c.pid7.unin <- rbind(ans.ci(predict(reg.c.pid7, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "union.member", 1), type="response")))
evs.c.pid7.unin$id.group <- c(rep("Non-\nmember", length(aware.p)), rep("Union\nmember", length(aware.p)))
evs.c.pid7.unin$aware.p <- aware.p  
gg.c.pid7.unin <- ggplot(evs.c.pid7.unin, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(i) Union membership") +
  geom_text(data=subset(evs.c.pid7.unin, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()


# (j) Party identity and veteran status
evs.c.pid7.vets <- rbind(ans.ci(predict(reg.c.pid7, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.pid7, replace(c.ctrl, "veteran", 1), type="response")))
evs.c.pid7.vets$id.group <- c(rep("Non-\nveteran", length(aware.p)), rep("Veteran", length(aware.p)))
evs.c.pid7.vets$aware.p <- aware.p  
gg.c.pid7.vets <- ggplot(evs.c.pid7.vets, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  geom_ribbon(alpha=.2) + ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(j) Veteran status") +
  geom_text(data=subset(evs.c.pid7.vets, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()


# Create Figure 2
f2_row1 <- gg.c.atts.relg + gg.c.atts.race + gg.c.atts.gend + gg.c.atts.unin + gg.c.atts.vets + plot_layout(ncol=5)
f2_row2 <- gg.c.pid7.relg + gg.c.pid7.race + gg.c.pid7.gend + gg.c.pid7.unin + gg.c.pid7.vets + plot_layout(ncol=5)
f2_title1 <- ggdraw() + draw_label("Predicted policy preferences", fontface='bold', size=16, hjust=.5)
f2_title2 <- ggdraw() + draw_label("Predicted party identity", fontface='bold', size=16, hjust=.5)

f2_title1 / f2_row1 / plot_spacer() / f2_title2 / f2_row2 + plot_layout(heights=c(.15, 1,.05,.15,1))
ggsave("Figure2.pdf", width=22, height=14)




# Specific estimates discussed in text

# Religious identities at different levels of awareness
print(subset(evs.c.atts.relg, aware.p==.1|aware.p==.9), digits=2) # policy preferences  
print(subset(evs.c.pid7.relg, aware.p==.1|aware.p==.9), digits=2) # party identity 

# Racial/ethnic identities at different levels of awareness
print(subset(evs.c.atts.race, aware.p==.1|aware.p==.9), digits=2) # policy preferences   
print(subset(evs.c.pid7.race, aware.p==.1|aware.p==.9), digits=2) # party identity

# Gender identities at different levels of awareness
print(subset(evs.c.atts.gend, aware.p==.1|aware.p==.9), digits=2) # policy preferences   
print(subset(evs.c.pid7.gend, aware.p==.1|aware.p==.9), digits=2) # party identity

# Union membership at different levels of awareness
print(subset(evs.c.pid7.unin, aware.p==.1|aware.p==.9), digits=2) # party identity
print(subset(evs.c.atts.unin, aware.p==.1|aware.p==.9), digits=2) # policy preferences   

# Veteran status at different levels of awareness
print(subset(evs.c.pid7.vets, aware.p==.1|aware.p==.9), digits=2) # party identity
print(subset(evs.c.atts.vets, aware.p==.1|aware.p==.9), digits=2) # policy preferences   





################################################################################
# IDENTITY GAPS INCREASE WITH AWARENESS
################################################################################

# Set new values of awareness used in simulations for 10th/50th/90th percentiles
gap.aware.p <- c(0.1,0.5,0.9)
c.ctrl <- data.frame(race="White", religion="Mainline Protestant", lgbt=0, union.member=0, veteran=0, female=1, age=47.9, married=1, income=2.81, educ=2.95, rel.atd=2.94, region="South", year="2016", aware.p=gap.aware.p)


# Simulate gaps between specific groups
gap.c.atts <- rbind(
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "race", "Black"))-predict(reg.c.atts, c.ctrl)),
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "race", "Hispanic"))-predict(reg.c.atts, c.ctrl)),
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "race", "Asian"))-predict(reg.c.atts, c.ctrl)),
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "religion", "Jewish"))-predict(reg.c.atts, replace(c.ctrl, "religion", "Evangelical Protestant"))),
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "religion", "Secular"))-predict(reg.c.atts, replace(c.ctrl, "religion", "Evangelical Protestant"))),
  ans.ci(predict(reg.c.atts, c.ctrl)-predict(reg.c.atts, replace(c.ctrl, "female", 0))),
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "lgbt", 1))-predict(reg.c.atts, c.ctrl)),
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "union.member", 1))-predict(reg.c.atts, c.ctrl)),
  ans.ci(predict(reg.c.atts, replace(c.ctrl, "veteran", 1))-predict(reg.c.atts, c.ctrl)))

gap.c.atts$aware.p <- gap.aware.p
gap.c.atts$aware.p <- factor(gap.c.atts$aware.p, levels=c("0.9", "0.5", "0.1"))
gap.c.atts$id <- c(rep("Black respondents",3), rep("Hispanic respondents",3), rep("Asian respondents",3), 
                   rep("Jewish respondents",3), rep("Secular respondents",3), 
                   rep("Women and men",3), 
                   rep("Straight and LGBT respondents",3), 
                   rep("Non-members and  union members",3),
                   rep("Non-veterans and veterans", 3))

# Create Table 2
gap.c.atts.table <- gap.c.atts %>%
  pivot_wider(names_from=aware.p, values_from=c(est,lo,hi)) %>%
  mutate(across(3:10, sprintf, fmt="%.2f")) %>%
  unite("ci10", c(lo_0.1, hi_0.1), sep=", ") %>%
  unite("ci50", c(lo_0.5, hi_0.5), sep=", ") %>%
  unite("ci90", c(lo_0.9, hi_0.9), sep=", ") %>%
  mutate(lb="[", rb="]") %>%
  unite("p10", c(lb,ci10,rb), sep="") %>%
  mutate(lb="[", rb="]") %>%
  unite("p50", c(lb,ci50,rb), sep="") %>%
  mutate(lb="[", rb="]") %>%
  unite("p90", c(lb,ci90,rb), sep="") %>%
  select(id,est_0.1,p10,est_0.5,p50,est_0.9,p90)
knitr::kable(gap.c.atts.table, format="latex", booktabs=T)







################################################################################
# HOW THE IMPACT OF AWARENESS VARIES ACROSS GROUPS
################################################################################

# Calculate the first difference in policy preferences between members of the same group at the 10th and 90th percentile of awareness

# Default simulation values at 10th percentile:
c.ctrl.lo <- data.frame(race="White", religion="Mainline Protestant", lgbt=0, union.member=0, veteran=0, female=1, age=47.9, married=1, income=2.81, educ=2.95, rel.atd=2.94, region="South", year="2016", aware.p=0.1)
# Default simulation values at 10th percentile:
c.ctrl.hi <- data.frame(race="White", religion="Mainline Protestant", lgbt=0, union.member=0, veteran=0, female=1, age=47.9, married=1, income=2.81, educ=2.95, rel.atd=2.94, region="South", year="2016", aware.p=0.9)

fds.wi.group <- rbind(
ans.ci(predict(reg.c.atts, c.ctrl.hi)-predict(reg.c.atts, c.ctrl.lo)),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "race", "Asian"))-predict(reg.c.atts, replace(c.ctrl.lo, "race", "Asian"))),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "race", "Black"))-predict(reg.c.atts, replace(c.ctrl.lo, "race", "Black"))),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "race", "Hispanic"))-predict(reg.c.atts, replace(c.ctrl.lo, "race", "Hispanic"))),
ans.ci(predict(reg.c.atts, c.ctrl.hi)-predict(reg.c.atts, c.ctrl.lo)),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "religion", "Catholic"))-predict(reg.c.atts, replace(c.ctrl.lo, "religion", "Catholic"))),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "religion", "Evangelical Protestant"))-predict(reg.c.atts, replace(c.ctrl.lo, "religion", "Evangelical Protestant"))),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "religion", "Jewish"))-predict(reg.c.atts, replace(c.ctrl.lo, "religion", "Jewish"))),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "religion", "Secular"))-predict(reg.c.atts, replace(c.ctrl.lo, "religion", "Secular"))),
ans.ci(predict(reg.c.atts, c.ctrl.hi)-predict(reg.c.atts, c.ctrl.lo)),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "lgbt", 1))-predict(reg.c.atts, replace(c.ctrl.lo, "lgbt", 1))),
ans.ci(predict(reg.c.atts, c.ctrl.hi)-predict(reg.c.atts, c.ctrl.lo)),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "female", 0))-predict(reg.c.atts, replace(c.ctrl.lo, "female", 0))),
ans.ci(predict(reg.c.atts, c.ctrl.hi)-predict(reg.c.atts, c.ctrl.lo)),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "union.member", 1))-predict(reg.c.atts, replace(c.ctrl.lo, "union.member", 1))),
ans.ci(predict(reg.c.atts, c.ctrl.hi)-predict(reg.c.atts, c.ctrl.lo)),
ans.ci(predict(reg.c.atts, replace(c.ctrl.hi, "veteran", 1))-predict(reg.c.atts, replace(c.ctrl.lo, "veteran", 1))))

fds.wi.group$abs.est <- abs(fds.wi.group$est) # Get absolute value to account for some being liberal leaning, others conservative
fds.wi.group$group <- c("White", "Asian", "Black", "Hispanic", "Mainline\nProtestant", "Catholic", "Evangelical\nProtestant", "Jewish", "Secular", "S/C",  "LGBT", "Women", "Men", "Nonmember", "Union member", "Non veteran", "Veteran")

# Specific estimates discussed in text:
fds.wi.group[order(-fds.wi.group$abs.est),]

# Select identities for which Egan (2020) has estimates of fluidity
fds.wi.group.egan <- fds.wi.group[c(1:9,11),]
fds.wi.group.egan$egan_switch <- c(5.6, 6.3, 7.3, 6.6,17.9, 13.6, 29.1, 18.5, 39.1, 46.5) # Estimate of group switch rate, from Egan (2020) SI Table 1
fds.wi.group.egan$egan_fx <- abs(c(0, 0, -.02, -.02, .07, .01, .08, -.01, -.10, -.02)) # Estimate of effect of politics on identity, from Egan (2020) Figure 2


# Creating Figure 3
g.egan1 <- ggplot(fds.wi.group.egan, aes(x=egan_switch, y=abs.est, label=group)) +
  geom_smooth(method="lm", se=F, fullrange=T, color="black") +
  geom_point() +
  geom_text_repel(nudge_x=0.2, size=3.5, lineheight=.8) +
  scale_x_continuous(limits=c(0,50), labels=c("0%", "10%", "20%", "30%", "40%", "50%")) +
  scale_y_continuous(limits=c(0,.25)) +
  ylab("Absolute difference between less- and more- aware members") + xlab("Egan (2020) estimate of share of group switching in/out of identity") +
  ggtitle("(a) by Egan (2020) estimate of group switch rate") +
  theme_idkn()

g.egan2 <- ggplot(fds.wi.group.egan, aes(x=egan_fx, y=abs.est, label=group)) +
  geom_smooth(method="lm", se=F, fullrange=T, color="black") +
  geom_point() +
  geom_text_repel(size=3.5, lineheight=.8, nudge_x=.001) +
  scale_x_continuous(limits=c(0,0.1), labels=c(".000", ".025", ".050", ".075", ".100")) +
  scale_y_continuous(limits=c(0,.25)) +
  ylab("Absolute difference between less- and more- aware members") + xlab("Egan (2020) estimate of effect of political views on identity") +
  ggtitle("(b) by Egan (2020) estimate of effect of politics on identity") +
  theme_idkn()

g.egan1 + plot_spacer() + g.egan2 + plot_layout(widths=c(1,.05,1))
ggsave("Figure3.pdf", height=6, width=12)
















################################################################################
################################################################################
#   ONLINE APPENDIX
################################################################################
################################################################################

# Additional libraries for appendix material
library(gtsummary)
library(texreg)

################################################################################
# A2.2 DESCRIPTIVE STATISTICS
################################################################################

# Descriptive statistics for CCES data, for Tables A2 and A4
tbl_svysummary(w.cces, 
               type=list(pid7~'continuous', aware.p~'continuous', lgbt='continuous', female='continuous', union.member='continuous', veteran='continuous', married='continuous', income='continuous', educ='continuous', rel.atd='continuous', ideo='continuous', vote='categorical'),
               statistic=list(all_continuous() ~ "{min} {max} {mean} {sd}"),
               digits=list(age~2),
               missing="no")

tbl_svysummary(w.cces, 
               type=list(lgbt='categorical', female='categorical', union.member='categorical', veteran='categorical'),
               statistic=list(all_continuous() ~ "{min} {max} {mean} {sd}",
                              all_categorical() ~"{n} ({p}%) {N}"),
               missing="no")




# Descriptive statistics for ANES data, for Tables A3 and A4
tbl_svysummary(w.anes, 
               type=list(pid7~'continuous', aware.p~'continuous', lgbt='continuous', female='continuous', union.member='continuous', veteran='continuous', married='continuous', income='continuous', educ='continuous', rel.atd='continuous', ideo='continuous', vote='categorical', lf.race='continuous', id.race='continuous', id.relg='continuous'),
               statistic=list(all_continuous() ~ "{min} {max} {mean} {sd}"),
               digits=list(age~2),
               missing="no")

tbl_svysummary(w.anes, 
               type=list(lgbt='categorical', female='categorical', union.member='categorical', veteran='categorical'),
               statistic=list(all_continuous() ~ "{min} {max} {mean} {sd}",
                              all_categorical() ~"{n} ({p}%) {N}"),
               missing="no")



################################################################################
# A3.5 PREDICTING POLITICAL AWARENESS
################################################################################

# Regression models for Table A5
c.reg.aware <- svyglm(aware~religion+race+female+lgbt+union.member+veteran+age+married+income+educ+rel.atd+region+year, w.cces)
a.reg.aware <- svyglm(aware~religion+race+female+lgbt+union.member+veteran+age+married+income+educ+rel.atd+region+year, w.anes)
texreg(list(c.reg.aware, a.reg.aware), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", include.deviance=F, include.dispersion=F)


################################################################################
# A4 REGRESSION MODELS
################################################################################

# Table A6: Main models predicting policy preferences and party identity
texreg(list(reg.c.atts, reg.c.pid7, reg.a.atts, reg.a.pid7), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", custom.model.names = c("Policy (CCES)", "Party (CCES)", "Policy (ANES)", "Party (ANES)"), include.deviance=F, include.dispersion=F)



# Table A7: Additional models predicting ideology and presidential vote choice 

reg.c.ideo <- svyglm(ideo~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region+year, w.cces)

reg.c.vote <- svyglm(vote~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region, w.cces, family=binomial(link="logit"))

reg.a.ideo <- svyglm(ideo~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region+year, w.anes)

reg.a.vote <- svyglm(vote~
                       race*aware.p+
                       religion*aware.p+
                       female*aware.p+
                       lgbt*aware.p+
                       union.member*aware.p+
                       veteran*aware.p+
                       age+married+income+educ+rel.atd+region, w.anes, family=binomial(link="logit"))

texreg(list(reg.c.ideo, reg.c.vote, reg.a.ideo, reg.a.vote), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", custom.model.names = c("Ideo (CCES)", "Vote (CCES)", "Ideo (ANES)", "Vote (ANES)"), include.deviance=F, include.dispersion=F)

# Comparing the sexuality gap across dependent variables
c.chet <- data.frame(race="White", religion="Mainline Protestant", lgbt=0, union.member=0, veteran=0, female=1, age=47.9, married=1, income=2.81, educ=2.95, rel.atd=2.94, region="South", year="2016", aware.p=.5)
c.lgbt <- data.frame(race="White", religion="Mainline Protestant", lgbt=1, union.member=0, veteran=0, female=1, age=47.9, married=1, income=2.81, educ=2.95, rel.atd=2.94, region="South", year="2016", aware.p=.5)

round(ans.ci(predict(reg.c.pid7, c.lgbt, type="response")-predict(reg.c.pid7, c.chet, type="response")),2)
round(ans.ci(predict(reg.c.ideo, c.lgbt, type="response")-predict(reg.c.ideo, c.chet, type="response")),2)
round(ans.ci(predict(reg.c.atts, c.lgbt, type="response")-predict(reg.c.atts, c.chet, type="response")),2)
round(ans.ci(predict(reg.c.vote, c.lgbt, type="response")-predict(reg.c.vote, c.chet, type="response")),2)





# Table A8: Additional models breaking out LGBT subgroups 
reg.c.pid7.lgbt <- svyglm(pid7~
                            lgb*aware.p+
                            trans*aware.p+
                            race*aware.p+
                            religion*aware.p+
                            female*aware.p+
                            union.member*aware.p+
                            veteran*aware.p+
                            age+married+income+educ+rel.atd+region+year, w.cces)

reg.c.atts.lgbt <- svyglm(atts~
                            lgb*aware.p+
                            trans*aware.p+
                            race*aware.p+
                            religion*aware.p+
                            female*aware.p+
                            union.member*aware.p+
                            veteran*aware.p+
                            age+married+income+educ+rel.atd+region+year, w.cces)

reg.a.pid7.lgbt <- svyglm(pid7~
                            lgb*aware.p+
                            race*aware.p+
                            religion*aware.p+
                            female*aware.p+
                            union.member*aware.p+
                            veteran*aware.p+
                            age+married+income+educ+rel.atd+region+year, w.anes)

reg.a.atts.lgbt <- svyglm(atts~
                            lgb*aware.p+
                            race*aware.p+
                            religion*aware.p+
                            female*aware.p+
                            union.member*aware.p+
                            veteran*aware.p+
                            age+married+income+educ+rel.atd+region+year, w.anes)

texreg(list(reg.c.atts.lgbt, reg.c.pid7.lgbt, reg.a.atts.lgbt, reg.a.pid7.lgbt), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", custom.model.names = c("Policy (CCES)", "Party (CCES)", "Policy (ANES)", "Party (ANES)"), include.deviance=F, include.dispersion=F)



# Table A9: Additional models using interest in politics in place of full awareness scale

reg.c.pid7.interest <- svyglm(pid7~
                                race*interest+
                                religion*interest+
                                female*interest+
                                lgbt*interest+
                                union.member*interest+
                                veteran*interest+
                                age+married+income+educ+rel.atd+region+year, w.cces)
reg.c.atts.interest <- svyglm(atts~
                                race*interest+
                                religion*interest+
                                female*interest+
                                lgbt*interest+
                                union.member*interest+
                                veteran*interest+
                                age+married+income+educ+rel.atd+region+year, w.cces)
reg.a.pid7.interest <- svyglm(pid7~
                                race*interest+
                                religion*interest+
                                female*interest+
                                lgbt*interest+
                                union.member*interest+
                                veteran*interest+
                                age+married+income+educ+rel.atd+region+year, w.anes)
reg.a.atts.interest <- svyglm(atts~
                                race*interest+
                                religion*interest+
                                female*interest+
                                lgbt*interest+
                                union.member*interest+
                                veteran*interest+
                                age+married+income+educ+rel.atd+region+year, w.anes)

texreg(list(reg.c.atts.interest,reg.c.pid7.interest, reg.a.atts.interest,reg.a.pid7.interest), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", custom.model.names = c("Policy (CCES)", "Party (CCES)", "Policy (ANES)", "Party (ANES)"), include.deviance=F, include.dispersion=F)




################################################################################
# A5 EVIDENCE OF AN INDIRECT IDENTITY-TO-POLITICS LINK 
################################################################################

# Create model of policy preferences with control for party/awareness interaction
reg.c.atts.pid <- svyglm(atts~
                           race*aware.p+
                           religion*aware.p+
                           female*aware.p+
                           lgbt*aware.p+
                           union.member*aware.p+
                           veteran*aware.p+
                           pid7*aware.p+
                           age+married+income+educ+rel.atd+region+year, w.cces)

# Create Table A10
texreg(list(reg.c.atts, reg.c.atts.pid), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", include.deviance=F, include.dispersion=F)

# Create Figure A1 based on new model
# New default values include party identity set to its mean

c.ctrl <- data.frame(pid7=.54, race="White", religion="Mainline Protestant", lgbt=0, union.member=0, veteran=0, female=1, age=47.9, married=1, income=2.81, educ=2.95, rel.atd=2.94, region="South", year="2016", aware.p=aware.p)


evs.c.atts.relg <- rbind(ans.ci(predict(reg.c.atts.pid, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "religion", "Catholic"), type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "religion", "Evangelical Protestant"), type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "religion", "Jewish"), type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "religion", "Secular"), type="response")))
evs.c.atts.relg$id.group <- c(rep("Mainline", length(aware.p)), rep("Catholic", length(aware.p)), rep("Evangelical\nProtestant", length(aware.p)), rep("Jewish", length(aware.p)), rep("Secular", length(aware.p)))  
evs.c.atts.relg$aware.p <- aware.p  
gg.c.atts.relg <- ggplot(evs.c.atts.relg, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("Predicted policy preferences") + ggtitle("(a) Religion") +
  geom_text(data=subset(evs.c.atts.relg, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, nudge_y=c(0,-.01,-.02,.005,0.015), lineheight=.7) + #
  coord_cartesian(clip='off') +
  theme_idkn2()

evs.c.atts.race <- rbind(ans.ci(predict(reg.c.atts.pid, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "race", "Black"), type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "race", "Hispanic"), type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "race", "Asian"), type="response")))
evs.c.atts.race$id.group <- c(rep("White", length(aware.p)), rep("Black", length(aware.p)), rep("Hispanic", length(aware.p)), rep("Asian", length(aware.p)))  
evs.c.atts.race$aware.p <- aware.p  
gg.c.atts.race <- ggplot(evs.c.atts.race, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(b) Race and ethnicity") +
  geom_text(data=subset(evs.c.atts.race, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()

evs.c.atts.gend <- rbind(ans.ci(predict(reg.c.atts.pid, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "female", 0), type="response")))
evs.c.atts.gend$id.group <- c(rep("Women", length(aware.p)), rep("Men", length(aware.p)))
evs.c.atts.gend$aware.p <- aware.p  
gg.c.atts.gend <- ggplot(evs.c.atts.gend, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(c) Gender") +
  geom_text(data=subset(evs.c.atts.gend, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()


evs.c.atts.unin <- rbind(ans.ci(predict(reg.c.atts.pid, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "union.member", 1), type="response")))
evs.c.atts.unin$id.group <- c(rep("Non-\nmember", length(aware.p)), rep("Union\nmember", length(aware.p)))
evs.c.atts.unin$aware.p <- aware.p  
gg.c.atts.unin <- ggplot(evs.c.atts.unin, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(d) Union membership") +
  geom_text(data=subset(evs.c.atts.unin, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, nudge_y=c(-.005,.005), lineheight=.7) + # 
  coord_cartesian(clip='off') +
  theme_idkn2()

evs.c.atts.vets <- rbind(ans.ci(predict(reg.c.atts.pid, c.ctrl, type="response")),
                         ans.ci(predict(reg.c.atts.pid, replace(c.ctrl, "veteran", 1), type="response")))
evs.c.atts.vets$id.group <- c(rep("Non-\nveteran", length(aware.p)), rep("Veteran", length(aware.p)))
evs.c.atts.vets$aware.p <- aware.p  
gg.c.atts.vets <- ggplot(evs.c.atts.vets, aes(x=aware.p, y=est, ymin=lo, ymax=hi, group=id.group, linetype=id.group)) +
  ylim(0,1) +
  scale_x_continuous(breaks=c(0,.25,.5,.75,1), labels=c(0,25,50,75,100)) +
  geom_line() + xlab("Political awareness percentile") + ylab("") + ggtitle("(e) Veteran status") +
  geom_text(data=subset(evs.c.atts.vets, aware.p==1), aes(label=id.group), hjust=0, nudge_x =0.02, nudge_y=c(.015,-0.005), lineheight=.7) +
  coord_cartesian(clip='off') +
  theme_idkn2()


p1 <- gg.c.atts.relg + gg.c.atts.race + gg.c.atts.gend + gg.c.atts.unin + gg.c.atts.vets + plot_layout(ncol=5)
title1 <- ggdraw() + draw_label("Predicted policy preferences", fontface='bold', size=16, hjust=.5)
title1 / p1 + plot_layout(heights=c(.15, 1))
ggsave("FigureA1.pdf", width=22, height=7)




################################################################################
# A6 IS AWARENESS JUST A PROXY FOR IDENTITY STRENGTH?
################################################################################

# Load data files and create weighted versions:
anes_appx_race <- readRDS("anes_appx_race.rds")
anes_appx_relg <- readRDS("anes_appx_relg.rds")
w.anes_appx_race <- svydesign(~1, data=anes_appx_race, weights=~weight)
w.anes_appx_relg <- svydesign(~1, data=anes_appx_relg, weights=~weight)


# Models that control for identity importance and linked fate with ANES data

# Religious identities:
reg.a.atts.relg <- svyglm(atts~
                               lgbt+religion+female+union.member+race+veteran+
                               religion*aware.p+
                               age+married+income+educ+rel.atd+region, w.anes_appx_relg)
reg.a.atts.relg.id <- svyglm(atts~
                               lgbt+religion+female+union.member+race+veteran+
                               id.relg*religion+
                               religion*aware.p+
                               age+married+income+educ+rel.atd+region, w.anes_appx_relg)

# Table A12
texreg(list(reg.a.atts.relg, reg.a.atts.relg.id), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", include.deviance=F, include.dispersion=F)



# Racial/ethnic identities:
reg.a.atts.race <- svyglm(atts~
                            lgbt+religion+female+union.member+race+veteran+
                            race*aware.p+
                            age+married+income+educ+rel.atd+region, w.anes_appx_race)
reg.a.atts.race.id <- svyglm(atts~
                               lgbt+religion+female+union.member+race+veteran+
                               id.race*race+
                               lf.race*race+
                               race*aware.p+
                               age+married+income+educ+rel.atd+region, w.anes_appx_race)

# Table A13
texreg(list(reg.a.atts.race, reg.a.atts.race.id), single.row=T, booktabs=T, caption.above=T, dcolumn=T, stars=c(.001, .01, .05, .1), symbol="\\dagger", fontsize="small", include.deviance=F, include.dispersion=F)

